package com.wltt.product.frfront.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public abstract class CodeGenerateUtil {

    private static final Random RANDOM = new Random();
    private static final String SIMPLE_DATE_FORMAT = "yyyyMMddHHmmss";
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";

    /**
     * 生成一定数量的随机数字组成字符串
     *
     * @param count 随机数的个数
     * @return 字符编码
     */
    public static String generateRandom(int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(RANDOM.nextInt(10));
        }
        return sb.toString();
    }

    /**
     * 生成一定长度的随机字符串
     *
     * @param length 字符串的长度
     * @return 字符编码
     */
    public static String generateStringCode(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
        }
        return sb.toString();
    }

    /**
     * 时间戳+一定数量的随机数
     *
     * @param count 随机数的个数
     * @return 字符编码
     */
    public static String generateTimestampRandom(int count) {
        SimpleDateFormat sdf = new SimpleDateFormat(SIMPLE_DATE_FORMAT);
        return sdf.format(new Date()) + generateRandom(count);
    }

    /**
     * 生成32位uuid
     *
     * @return 字符编码
     */
    public static String generateUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static void main(String[] args) {
        System.out.println(generateUUID());
        System.out.println(generateTimestampRandom(1));
    }

}
